package com.virgo.common.utils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class InviteCodeUtils {

	/**
	 * 32个进制字符（0,1 没加入，容易和 o l 混淆，O 未加入，F 未加入，用于补位）
	 * 顺序可进行调整, 增加反推难度
	 */
	private final String[] base = new String[]{
			"H", "V", "E", "8", "S", "2", "D", "Z", "X", "9", "C", "7", "P","5", "I", "K", "3", "M", "J", "U", "A", "R", "4", "W", "Y", "L", "T", "N", "6", "B", "G", "Q"
	};

	/**
	 * F为补位字符，不能和上述字符重复
	 */
	private final String pad = "F";

	private final int decimalLen = 32;

	/**
	 * 生成code的长度
	 */
	private final int codeMinLen = 8;

	public String getCode(long id){
		StringBuilder code = new StringBuilder();
		if (id <= 0) {
			return code.toString();
		}

		while ( ( id / decimalLen) > 0) {
			int index = (int) (id % decimalLen);
			code.append(base[index]);
			id = id / decimalLen;
		}
		int index = (int) (id % decimalLen);
		code.append(base[index]);
		int codeLength = code.length();
		if (codeLength < codeMinLen) {
			code.append(pad);
			for (int i = 0 ; i < ( codeMinLen - codeLength - 1 ) ; i++ ) {
				int rand = ICommonUtils.getRandom(0,decimalLen-1);
				code.append(base[rand]);
			}
		}
		return code.toString();
	}

}
